package com.note.gateway.config;

import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * 网关Feign配置
 * 解决WebFlux环境下使用OpenFeign的问题
 */
@Slf4j
@Configuration
@LoadBalancerClients({
    @LoadBalancerClient(name = "note-app")
})
public class GatewayFeignConfig {

    /**
     * 负载均衡的WebClient
     */
    @Bean
    @LoadBalanced
    public WebClient.Builder loadBalancedWebClientBuilder() {
        return WebClient.builder();
    }

    /**
     * Jackson编码器
     */
    @Bean
    public Encoder feignEncoder() {
        return new JacksonEncoder();
    }

    /**
     * Jackson解码器
     */
    @Bean
    public Decoder feignDecoder() {
        return new JacksonDecoder();
    }

    /**
     * Feign客户端构建器
     */
    @Bean
    public Feign.Builder feignBuilder() {
        return Feign.builder()
                .encoder(feignEncoder())
                .decoder(feignDecoder())
                .logLevel(Logger.Level.FULL);
    }
} 